using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

using Plain;
using Plain.Data;
using Plain.Search;
using Plain.Validation;
using Plain.Web.Mvc.Controllers;
using Plain.Utilities;
using Plain.Web.Mvc;

using Plain.Demo.Blog.Business;
using Plain.Demo.Blog.Web;
using System.Web.Security;

namespace Controllers
{
	[HandleError]
	public class BlogController : PlainController<Post>
	{
		public ActionResult Index ()
		{
			return View(p["Published"] <= DateTime.Now.Date);
		}
		
		public ActionResult Details (int id){
			var post = Get(id);
			return View (post).Add<Post>("posts", p["Published"] <= DateTime.Now.Date);
		}
		
		public ActionResult ByTag (string id){
			return View (p["Tags"].Contains(id) & p["Published"] != null & p["Published"] <= DateTime.Now.Date);
		}
		
		public ActionResult List(int year, string month){
			int monthNum = DateTime.ParseExact(month, "MMMM", new CultureInfo("en-us")).Month;
			var posts = Search(p["Published"] != null & p["Published"] <= DateTime.Now.Date & p["Published.Value.Year"] == year & p["Published.Value.Month"] == monthNum);
			return View (posts);
		}
		
		public ActionResult About (){			
			var User = Get<User>(1);
			return View(User);
		}
				
		public RssResult Feed(){
			var posts = Search(p["Published"] != null & p["Published"] <= DateTime.Now.Date);
			return new RssResult() { Feed = posts.ToFeed("Blogspot of Kristian Gundry", Url.Action("Index","Blog"), Url.Action("Details","Blog"), "Kristian Gundry's Blog about programming in Microsoft.Net", "en-us") };
		}
		
		[AcceptVerbs(HttpVerbs.Post)]
		public PlainJsonResult CreateComment(int postId, string author, string email, string content){
			Comment comment = new Comment();
			comment.Author = author;
			comment.Email = email;
			comment.IpAddress = this.Request.UserHostAddress;
			comment.Content = content;
			comment.Created = DateTime.Now;
			comment.Post = Get(postId);
            if (comment.Validate()) { comment.Save(); }
			
			
			return new PlainJsonResult(){ Data=comment };
		}		
				
		public JsonResult Year(int? year){
			var posts = GetAll();
			IDictionary<string,int> years = new Dictionary<string, int>();
			
			for(int i=0;i<6;i++){
				int yearNum = (int)(year.Value - i);
				years.Add(yearNum.ToString(), posts.Where(x=>x.Published != null && x.Published.Value.Date <= DateTime.Now.Date && x.Published.Value.Year==yearNum).Count());
			}
			
			return Json(years);
		}
		
		
		public JsonResult Month(int? year,int? month){
			var posts = GetAll();
			
			IDictionary<string,int> Months = new Dictionary<string, int>();
			for(int i=0;i<6;i++){
				int monthNum = (int)(month + i);
				DateTime date = new DateTime((int)year,monthNum,1);
				Months.Add(date.ToString("MMMM",new CultureInfo("en-us")), posts.Where(x=> x.Published != null && x.Published.Value.Date <= DateTime.Now.Date && x.Published.Value.Month == monthNum && x.Published.Value.Year == year).Count());
			}
			
			return Json(Months);
		}
		
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult LogOn(string username, string password){
			if(FormsAuthentication.Authenticate(username,password)){
				FormsAuthentication.SetAuthCookie(username,false);
				return RedirectToAction("Index", "Blog");
			}
			return new HttpUnauthorizedResult();
		}
		
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult LogOff(){
			FormsAuthentication.SignOut();
			return RedirectToAction("Index", "Blog");
		}
	}
}